package com.module.TraceSource.manager;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.Page;

import com.module.TraceSource.dao.TsEntEventDao;
import com.module.TraceSource.dao.TsEntFlowDao;
import com.module.TraceSource.model.TraceSourceBeanUtil;
import com.module.TraceSource.util.ReturnUtil;
import com.module.TraceSource.vo.TsDefEvent;
import com.module.TraceSource.vo.TsDefFlow;
import com.module.TraceSource.vo.TsEntEvent;
import com.module.TraceSource.vo.TsEntFlow;

public class TsEntFlowManager extends GenericHibernateManager<TsEntFlow,Long>{
	private TsEntFlowDao dao = TsEntFlowDao.getInstance();
//	private TsDefFlowDao tsDefFlowDao = TsDefFlowDao.getInstance();
//	private TsDefEventDao tsDefEventDao = TsDefEventDao.getInstance();
	private TsEntEventDao tsEntEventDao = TsEntEventDao.getInstance();
	private static TsEntFlowManager service;

	private TsEntFlowManager(){
		super();
	}

	public synchronized static TsEntFlowManager getInstance(){
		if(service == null){
			service = new TsEntFlowManager();
		}
		return service;
	}

	@Override
	public GenericHibernateDao<TsEntFlow, Long> getDao() {
		return dao;
	}

	/**
	 * 根据组织查询设备流程
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public List findEntFlowByOrgId(String orgid) throws Exception{
		Session session = this.getSession();
		List<TsEntFlow> list = null;
		try{
			list = dao.findEntFlowByOrgId(session, orgid);
			this.closeSession();
		}catch(Exception e){
			this.closeSession();
		}
		return list;
	}


//	/**
//	 * 保存企业溯源流程,
//	 * 对应opt:saveTsEntFlow
//	 * @param bean
//	 * @return
//	 * @throws Exception
//	 */
//	public TraceSourceBeanUtil saveTsEntFlow(TraceSourceBeanUtil bean) throws Exception{
//		Session session = this.getSession();
//		boolean isTrue = false;
//		Transaction tx = session.beginTransaction();
//		TsEntFlow tsEntFlow = bean.getTsEntFlow();
//		String name = tsEntFlow.getFlowName().trim();
//		try{
//			boolean isExist=dao.isExistFlow(session,name, tsEntFlow.getOrgid(),tsEntFlow.getId());
//			if(isExist){
//				bean.setResult(ReturnUtil.optCodeErrorHandle("【"+name+"】已经存在!"));
//			}else{
//				tsEntFlow.setFlowName(name);
//				if(tsEntFlow.getId()==null || tsEntFlow.getId()==0){
//					tsEntFlow.setStatus(CUDEnum.C.toString());
//					tsEntFlow.setCreatetime(new Date());
//					isTrue = dao.save(session, tsEntFlow);
//				}else{
//					tsEntFlow.setStatus(CUDEnum.U.toString());
//					isTrue = dao.update(session, tsEntFlow);
//				}
//				tx.commit();
//				bean.setResult(ReturnUtil.optHandle(isTrue));
//			}}catch (Exception e) {
//				isTrue = false;
//				tx.rollback();
//				throw e;
//			}finally{
//				this.closeSession();
//			}
//			return bean;
//	}
//
//	/**
//	 * 分页列出企业流程列表,
//	 * 对应opt:listTsEntFlowPage
//	 * @param bean
//	 * @return
//	 * @throws Exception
//	 */
//	public TraceSourceBeanUtil listTsEntFlowPage(TraceSourceBeanUtil bean) throws Exception{
//		Session session = this.getSession();
//		Page pagePro = null;
//		try{
//			pagePro =  dao.getList(session,bean.getTsEntFlow(),bean.getPage());
//		}catch(Exception e){
//			throw e;
//		}finally{
//			this.closeSession();
//		}
//		bean.setPage(pagePro);
//		return bean;
//	}
//
//	/**
//	 * 通过id查出某一个企业溯源流程
//	 * 对应opt:findByIdTsEntFlow
//	 * @param bean
//	 * @return
//	 * @throws Exception
//	 */
//	public TraceSourceBeanUtil findByIdTsEntFlow(TraceSourceBeanUtil bean) throws Exception{
//		Session session = this.getSession();
//		try{
//			TsEntFlow tsEntFlow = dao.get(session, bean.getTsEntFlow().getId());
//			bean.setTsEntFlow(tsEntFlow);
//		}catch(Exception e){
//			throw e;
//		}finally{
//			this.closeSession();
//		}
//		return bean;
//	}
//
//	/**
//	 * 删除某一个企业溯源流程
//	 * 对应opt:removeTsEntFlow
//	 * @param bean
//	 * @return
//	 * @throws Exception
//	 */
//	public TraceSourceBeanUtil removeTsEntFlow(TraceSourceBeanUtil bean) throws Exception{
//		Session session = this.getSession();
//		boolean flag = false;
//		Transaction tx = session.beginTransaction();
//		try{
//			TsEntFlow tsEntFlow = dao.get(session, bean.getTsEntFlow().getId());
//			boolean isExist=tsEntEventDao.isExistsByFlowId(session, tsEntFlow.getId());
//			if(!isExist){
//				tsEntFlow.setStatus(CUDEnum.D.toString());
//				flag = dao.update(session, tsEntFlow);
//				tx.commit();
//				bean.setResult(ReturnUtil.optHandle(flag));
//			}else{
//				bean.setResult(ReturnUtil.optCodeErrorHandle(tsEntFlow.getFlowName()+"不能被删除"));
//			}
//		}catch(Exception e){
//			tx.rollback();
//			throw e;
//		}finally{
//			this.closeSession();
//		}
//		return bean;
//	}
//
//
//
//
//
//	/**
//	 * 从标准流程中复制出一个流程给企业
//	 * 对应opt:removeTsEntFlow
//	 * @param bean
//	 * @return
//	 * @throws Exception
//	 */
//	public TraceSourceBeanUtil copyDefFlow(TraceSourceBeanUtil bean) throws Exception{
//		boolean isTrue = false;
//		Session session = this.getSession();
//		Transaction tx = session.beginTransaction();
//		TsEntFlow paramEntFlow = bean.getTsEntFlow();
//		try{
//			TsEntFlow newTsEntFlow = buildEntFlowVo(session,paramEntFlow);
//			dao.save(session, newTsEntFlow);
//			List<TsDefEvent> defEventList = tsDefEventDao.findBySQL(session, "FROM TsDefEvent obj WHERE obj.status!='D' AND obj.flowid="+newTsEntFlow.getSourceid());
//			tsEntEventDao.saveEntEventList(session,newTsEntFlow,defEventList);
//			isTrue=true;
//			tx.commit();
//		}catch (Exception e) {
//			e.printStackTrace();
//			isTrue = false;
//			tx.rollback();
//			throw e;
//		}finally{
//			this.closeSession();
//		}
//
//		bean.setResult(ReturnUtil.optHandle(isTrue));
//		return bean;
//	}
//
//	/**
//	 * 从entFlow参数中生成一个新的tsEntFlow;
//	 * @param session 
//	 * @param paramEntFlow
//	 * @return
//	 */
//	private TsEntFlow buildEntFlowVo(Session session, TsEntFlow paramEntFlow) {
//		TsEntFlow entFlow = new TsEntFlow();
//		entFlow.setStatus(CUDEnum.C.toString());
//		entFlow.setOrgid(paramEntFlow.getOrgid());
//		entFlow.setOrgname(paramEntFlow.getOrgname());
//		entFlow.setCreaterid(paramEntFlow.getCreaterid());
//		entFlow.setCreatetime(new Date());
//		//paramEntFlow的id为defFlow的id;
//		TsDefFlow defFlow = tsDefFlowDao.get(session, paramEntFlow.getId());
//		entFlow.setFlowName(defFlow.getFlowName());
//		entFlow.setSourceid(defFlow.getId());
//		return entFlow;
//	}
}
